import {messages,queryCurrencyPrecision} from "share/common";
import React from 'react'
import { connect } from 'react-redux'
import { Form, Icon } from 'antd'

class LoanRepaymentAmount extends React.Component{
  constructor(props) {
    super(props);
    this.state = {
      buttonRoleSwitch:this.props.isOwner?true:this.checkPageRole('LOANBILLMANAGEMENT', 2) && this.checkFunctionProfiles(['er.disabled'], [[false, undefined]]) && this.checkFunctionProfiles(['finance.audit.disabled'], [[false, undefined]]) //按钮操作权限
    }
  }

  //新建还款
  toRepayment = () => {
    this.props.handleToRepayment()
  };

  render() {
    const { info } = this.props;
    const { buttonRoleSwitch } = this.state;
    let repaymentInfo = info.writeoffArtificialDTO || {}; //还款信息
    return (
      (info.status === 1005 || info.status === 1006 || info.status === 1007) && (
        <span className="loan-repayment-amount" style={{marginLeft:"10px"}}>
          {repaymentInfo.hasWriteoffAmount !== info.originCurrencyTotalAmount && (
            <span>
              {repaymentInfo.hasWriteoffAmount ?
                `${messages('request.key118'/*已还款*/)}：${repaymentInfo.currencyCode}  ${this.filterMoney(repaymentInfo.hasWriteoffAmount,queryCurrencyPrecision(repaymentInfo.currencyCode),true)}` : ''}
              {repaymentInfo.hasWriteoffAmount ? <span className="ant-divider"/> : ''}
              {repaymentInfo.lockedWriteoffAmount ?
                <span className='default-color-success'>
                  {messages('request.key119'/*还款中*/)}：{repaymentInfo.currencyCode}&nbsp; {this.filterMoney(repaymentInfo.lockedWriteoffAmount,queryCurrencyPrecision(repaymentInfo.currencyCode))}
                </span> : ''}
              {repaymentInfo.lockedWriteoffAmount && repaymentInfo.stayWriteoffAmount ? <span className="ant-divider"/> : ''}
              {repaymentInfo.stayWriteoffAmount ? (
                <span>
                  <span className='default-color-font' style={{marginRight: 10}}>
                    {messages('request.key120'/*待还款*/)}：{repaymentInfo.currencyCode}&nbsp; {this.filterMoney(repaymentInfo.stayWriteoffAmount,queryCurrencyPrecision(repaymentInfo.currencyCode))}
                  </span>
                  {buttonRoleSwitch && <a onClick={this.toRepayment}>{messages('request.key121'/*新建还款*/)} ></a>}
                </span>
              ) : ''}
            </span>
          )}
          {repaymentInfo.hasWriteoffAmount === info.originCurrencyTotalAmount && (
            <span className="repayment-all">
              <Icon type="check-circle-o" className="repayment-all-icon" />{messages('request.key122'/*已全额还款*/)}
            </span>
          )}
        </span>
      )
    )
  }
}

LoanRepaymentAmount.propTypes = {
  info: React.PropTypes.object,
  isOwner: React.PropTypes.bool,//是否为登录人控件，涉及权限
  handleToRepayment: React.PropTypes.func, //新建还款
};

LoanRepaymentAmount.defaultProps={
  info: {},
  isOwner: false,
  handleToRepayment: () => {}
};

function mapStateToProps() {
  return { }
}

const wrappedLoanRepaymentAmount = Form.create()(LoanRepaymentAmount);

export default connect(mapStateToProps)(wrappedLoanRepaymentAmount)
